package com.htg.tdtwebapi;

import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TdtApiTransitPlanning extends TdtWebApi{
    public static final String url = "http://api.tianditu.gov.cn/transit";
    public TdtApiTransitPlanning(String key) {
        super(key);
    }

    /**
     *
     * @param startPosition 出发点坐标 “经度，纬度”
     * @param endPosition   终点坐标 “经度，纬度”
     * @param lineType      获取线路规划类型(按位判断规划类型，以支持同时获取多种规划结果)第0位为1，较快捷；第1位为1，少换乘；第2位为1，少步行；第3位为1，不坐地铁。
     * @return
     * @throws Exception
     */
    public String planning(String startPosition,String endPosition,String lineType) throws Exception {
        //
        boolean legal = isParamsLegal(startPosition,endPosition,lineType);
        if (!legal)
            throw new Exception("startPosition,endPosition,lineType 为必填参数 不能为null 也不能为空串");
        //
        Map<String, Object> params = new HashMap<>();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("startposition", startPosition);
        jsonObject.put("endposition", endPosition);
        jsonObject.put("linetype", lineType);
        params.put("postStr", jsonObject.toString());
        params.put("tk", key);
        String response = get(url, params);
        return response;
    }


    /**
     *
     * @param startPosition 出发点坐标 “经度，纬度”
     * @param endPosition   终点坐标 “经度，纬度”
     * @param lineType      获取线路规划类型(按位判断规划类型，以支持同时获取多种规划结果)第0位为1，较快捷；第1位为1，少换乘；第2位为1，少步行；第3位为1，不坐地铁。
     * @return
     * @throws Exception
     */
    public List<Result> planning2(String startPosition,String endPosition,String lineType) throws Exception {
        //
        String response = planning(startPosition, endPosition, lineType);
        return response2Result(response);
    }


    /**
     * 根据提供的docid 搜索内容，主要是根据前端给的id 搜索公交站和公交线的详细信息
     * @param uuid
     * @return
     */
    public String busline(String uuid) throws Exception {
        //
        boolean legal = isParamsLegal(uuid);
        if (!legal)
            throw new Exception("uuid 为必填参数 不能为null 也不能为空串");
        //
        Map<String, Object> params = new HashMap<>();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("uuid", uuid);
        params.put("postStr", jsonObject.toString());
        params.put("tk", key);
        params.put("type", "busline");
        String response = get(url, params);
        return response;
    }

    /**
     * 根据提供的docid 搜索内容，主要是根据前端给的id 搜索公交站和公交线的详细信息
     * @param uuid
     * @return
     */
    public BusLine busline2(String uuid) throws Exception {
        //
        String response = busline(uuid);
        Gson gson = new Gson();
        return gson.fromJson(response, BusLine.class);
    }

    /**
     *
     * @param uuid
     * @return
     * @throws Exception
     */
    public String station(String uuid) throws Exception {
        //
        boolean legal = isParamsLegal(uuid);
        if (!legal)
            throw new Exception("uuid 为必填参数 不能为null 也不能为空串");
        //
        Map<String, Object> params = new HashMap<>();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("uuid", uuid);
        params.put("postStr", jsonObject.toString());
        params.put("tk", key);
        params.put("type", "busline");
        String response = get(url, params);
        return response;
    }


    /**
     *
     * @param uuid
     * @return
     * @throws Exception
     */
    public Station station2(String uuid) throws Exception {
        //
        String response = station(uuid);
        Gson gson = new Gson();
        return gson.fromJson(response, Station.class);
    }


    /**
     * 站点返程线路查询是查询经过一个站点的线路是否有反向的线路，即查询经过此战的此线路是否为双向成对的线路。
     * @param lineUuid
     * @param stationUuid
     * @return  如果一个公交线路上的某个站点 上 存在当前线路的反向线路 那么返回反向线路，否则返回null
     */
    public String reverseBusLine(String lineUuid,String stationUuid) throws Exception {
        //
        boolean legal = isParamsLegal(lineUuid,stationUuid);
        if (!legal)
            throw new Exception("lineUuid,stationUuid 为必填参数 不能为null 也不能为空串");
        //
        Map<String, Object> params = new HashMap<>();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("lineUuid", lineUuid);
        jsonObject.put("stationUuid", stationUuid);
        params.put("postStr", jsonObject.toString());
        params.put("tk", key);
        params.put("type", "busline");
        String response = get(url, params);
        return response;
    }


    /**
     * 站点返程线路查询是查询经过一个站点的线路是否有反向的线路，即查询经过此战的此线路是否为双向成对的线路。
     * @param lineUuid
     * @param stationUuid
     * @return 如果一个公交线路上的某个站点 上 存在当前线路的反向线路 那么返回反向线路，否则返回null
     */
    public BusLine reverseBusLine2(String lineUuid,String stationUuid) throws Exception {
        //
        String response = reverseBusLine(lineUuid,stationUuid);
        Gson gson = new Gson();
        return gson.fromJson(response, BusLine.class);
    }


    private List<Result> response2Result(String response) throws Exception {
        List<Result> results = new ArrayList<>();
        //
        JSONObject jsonObject = new JSONObject(response);
        int resultCode = jsonObject.getInt("resultCode");

        if (resultCode == 0) {
            JSONArray jsonArray = jsonObject.getJSONArray("results");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject result = jsonArray.getJSONObject(i);
                Gson gson = new Gson();
                results.add(gson.fromJson(result.toString(), Result.class));
            }
        } else {
            throw new Exception("公交规划异常");
        }

        return results;
    }

    public class BusLine implements Serializable{
        private int ticketcal;
        private int ismanual;
        private String linepoint;
        private int linetype;
        private int totalprice;
        private String byuuid;
        private int length;
        private int ismonticket;
        private String endtime;
        private int increasedstep;
        private String starttime;
        private int startprice;
        private int increasedprice;
        private int stationnum;
        private int totaltime;
        private List<Station> station;
        private int isbidirectional;
        private String linename;
        private int interval;
        private String company;

        public int getTicketcal() {
            return ticketcal;
        }

        public void setTicketcal(int ticketcal) {
            this.ticketcal = ticketcal;
        }

        public int getIsmanual() {
            return ismanual;
        }

        public void setIsmanual(int ismanual) {
            this.ismanual = ismanual;
        }

        public String getLinepoint() {
            return linepoint;
        }

        public void setLinepoint(String linepoint) {
            this.linepoint = linepoint;
        }

        public int getLinetype() {
            return linetype;
        }

        public void setLinetype(int linetype) {
            this.linetype = linetype;
        }

        public int getTotalprice() {
            return totalprice;
        }

        public void setTotalprice(int totalprice) {
            this.totalprice = totalprice;
        }

        public String getByuuid() {
            return byuuid;
        }

        public void setByuuid(String byuuid) {
            this.byuuid = byuuid;
        }

        public int getLength() {
            return length;
        }

        public void setLength(int length) {
            this.length = length;
        }

        public int getIsmonticket() {
            return ismonticket;
        }

        public void setIsmonticket(int ismonticket) {
            this.ismonticket = ismonticket;
        }

        public String getEndtime() {
            return endtime;
        }

        public void setEndtime(String endtime) {
            this.endtime = endtime;
        }

        public int getIncreasedstep() {
            return increasedstep;
        }

        public void setIncreasedstep(int increasedstep) {
            this.increasedstep = increasedstep;
        }

        public String getStarttime() {
            return starttime;
        }

        public void setStarttime(String starttime) {
            this.starttime = starttime;
        }

        public int getStartprice() {
            return startprice;
        }

        public void setStartprice(int startprice) {
            this.startprice = startprice;
        }

        public int getIncreasedprice() {
            return increasedprice;
        }

        public void setIncreasedprice(int increasedprice) {
            this.increasedprice = increasedprice;
        }

        public int getStationnum() {
            return stationnum;
        }

        public void setStationnum(int stationnum) {
            this.stationnum = stationnum;
        }

        public int getTotaltime() {
            return totaltime;
        }

        public void setTotaltime(int totaltime) {
            this.totaltime = totaltime;
        }

        public List<Station> getStation() {
            return station;
        }

        public void setStation(List<Station> station) {
            this.station = station;
        }

        public int getIsbidirectional() {
            return isbidirectional;
        }

        public void setIsbidirectional(int isbidirectional) {
            this.isbidirectional = isbidirectional;
        }

        public String getLinename() {
            return linename;
        }

        public void setLinename(String linename) {
            this.linename = linename;
        }

        public int getInterval() {
            return interval;
        }

        public void setInterval(int interval) {
            this.interval = interval;
        }

        public String getCompany() {
            return company;
        }

        public void setCompany(String company) {
            this.company = company;
        }
    }


    public class Result implements Serializable{
        private int lineType;
        private List<Line> lines;

        public int getLineType() {
            return lineType;
        }

        public void setLineType(int lineType) {
            this.lineType = lineType;
        }

        public List<Line> getLines() {
            return lines;
        }

        public void setLines(List<Line> lines) {
            this.lines = lines;
        }
    }

    public class Line implements Serializable{
        private List<Segment> segments;
        private String lineName;

        public List<Segment> getSegments() {
            return segments;
        }

        public void setSegments(List<Segment> segments) {
            this.segments = segments;
        }

        public String getLineName() {
            return lineName;
        }

        public void setLineName(String lineName) {
            this.lineName = lineName;
        }
    }

    public class Segment implements Serializable{
        private List<SegmentLine> segmentLine;
        private StationEnd stationEnd;
        private int segmentType;
        private StationStart stationStart;

        public List<SegmentLine> getSegmentLine() {
            return segmentLine;
        }

        public void setSegmentLine(List<SegmentLine> segmentLine) {
            this.segmentLine = segmentLine;
        }

        public StationEnd getStationEnd() {
            return stationEnd;
        }

        public void setStationEnd(StationEnd stationEnd) {
            this.stationEnd = stationEnd;
        }

        public int getSegmentType() {
            return segmentType;
        }

        public void setSegmentType(int segmentType) {
            this.segmentType = segmentType;
        }

        public StationStart getStationStart() {
            return stationStart;
        }

        public void setStationStart(StationStart stationStart) {
            this.stationStart = stationStart;
        }
    }

    public class SegmentLine implements Serializable{
        private String segmentStationCount;
        private int segmentTime;
        private int segmentTransferTime;
        private double segmentDistance;
        private String direction;
        private String SEndTime;
        private String linePoint;
        private String lineName;
        private String byuuid;

        public String getSegmentStationCount() {
            return segmentStationCount;
        }

        public void setSegmentStationCount(String segmentStationCount) {
            this.segmentStationCount = segmentStationCount;
        }

        public int getSegmentTime() {
            return segmentTime;
        }

        public void setSegmentTime(int segmentTime) {
            this.segmentTime = segmentTime;
        }

        public int getSegmentTransferTime() {
            return segmentTransferTime;
        }

        public void setSegmentTransferTime(int segmentTransferTime) {
            this.segmentTransferTime = segmentTransferTime;
        }

        public double getSegmentDistance() {
            return segmentDistance;
        }

        public void setSegmentDistance(double segmentDistance) {
            this.segmentDistance = segmentDistance;
        }

        public String getDirection() {
            return direction;
        }

        public void setDirection(String direction) {
            this.direction = direction;
        }

        public String getSEndTime() {
            return SEndTime;
        }

        public void setSEndTime(String SEndTime) {
            this.SEndTime = SEndTime;
        }

        public String getLinePoint() {
            return linePoint;
        }

        public void setLinePoint(String linePoint) {
            this.linePoint = linePoint;
        }

        public String getLineName() {
            return lineName;
        }

        public void setLineName(String lineName) {
            this.lineName = lineName;
        }

        public String getByuuid() {
            return byuuid;
        }

        public void setByuuid(String byuuid) {
            this.byuuid = byuuid;
        }
    }

    public class StationEnd extends Station{

    }

    public class StationStart extends Station {

    }

    public class Station implements Serializable{
        protected String lonlat;
        protected String name;
        protected String uuid;
        protected List<LineData> linedata;

        public String getLonlat() {
            return lonlat;
        }

        public void setLonlat(String lonlat) {
            this.lonlat = lonlat;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getUuid() {
            return uuid;
        }

        public void setUuid(String uuid) {
            this.uuid = uuid;
        }

        public List<LineData> getLinedata() {
            return linedata;
        }

        public void setLinedata(List<LineData> linedata) {
            this.linedata = linedata;
        }
    }

    public class LineData implements Serializable{
        private String name;
        private String uuid;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getUuid() {
            return uuid;
        }

        public void setUuid(String uuid) {
            this.uuid = uuid;
        }
    }

}
